//
//  TestUIPageControl.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/10/6.
//  Copyright © 2020 moon. All rights reserved.
//

#import "TestUIPageControl.h"
#import <MKUI/MKUI.h>
@interface TestUIPageControl()
@property(nonatomic,strong) UIPageControl *systemPageControl;
@property(nonatomic,strong) UILabel *pageLabel;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end

@implementation TestUIPageControl
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.systemPageControl = [[UIPageControl alloc] init];
        self.systemPageControl.hidesForSinglePage = YES;
        self.systemPageControl.currentPageIndicatorTintColor = [UIColor systemBlueColor];
        self.systemPageControl.pageIndicatorTintColor = [UIColor systemGrayColor];
        [self addSubview:self.systemPageControl];
        
        self.pageLabel = [[UILabel alloc] init];
        self.pageLabel.textColor = [UIColor systemBlueColor];
        self.pageLabel.textAlignment = NSTextAlignmentCenter;
        self.pageLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.pageLabel];
    }
    return self;
}
- (void)setHidesForSinglePage:(BOOL)hidesForSinglePage{
    self.systemPageControl.hidesForSinglePage = hidesForSinglePage;
}
- (BOOL)hidesForSinglePage{
    return self.systemPageControl.hidesForSinglePage;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    
    @MK_WEAKIFY(self);
    MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.systemPageControl sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
        @MK_NORMALIZE(self);
        CGSize s1 = [self.pageLabel intrinsicContentSize];
        CGSize s = s1;
        NSInteger pageShows = MIN(9,self.numberOfPages);
        UIPageControl *pageControl = wrapper.originItem;
        s = [pageControl sizeForNumberOfPages:pageShows];
//            s.width = MIN(s.width,100);
        s.width = MIN(size.width-s1.width-self.flowlayout.interitemSpacing,s.width);
        return s;
    }];
    MKUILayoutConstraintItemWrapper *w2 = [MKUILayoutConstraintItemWrapper wrapItem:self.pageLabel sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
        CGSize s1 = [wrapper.originItem sizeThatFits:size];
        s1.width = MAX(s1.width,40);
        return s1;
    }];
    //
    self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1,w2] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_C) contentInsets:UIEdgeInsetsZero interitemSpacing:5];
    
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 60);
}
- (void)setCurrentPage:(NSInteger)currentPage{
    _currentPage = currentPage;
    [self reloadData];
}
- (void)setNumberOfPages:(NSInteger)numberOfPages{
    _numberOfPages = numberOfPages;
    [self reloadData];
}
- (void)reloadData{
    self.systemPageControl.numberOfPages = self.numberOfPages;
    self.systemPageControl.currentPage = self.currentPage;
    
    self.pageLabel.text = [NSString stringWithFormat:@"%@/%@",@(self.numberOfPages?self.currentPage+1:0),@(self.numberOfPages)];
    [self setNeedsLayout];
}
@end

#import "MKUITestImagesPageControl.h"
@implementation TestUIPageControl_0

- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.pageControl = [[[self.class pageControlClass] alloc] init];
        self.pageControl.hidesForSinglePage = YES;
        self.pageControl.currentPageIndicatorTintColor = [UIColor systemBlueColor];
        self.pageControl.pageIndicatorTintColor = [UIColor systemGrayColor];
        [self addSubview:self.pageControl];
        
        self.pageLabel = [[UILabel alloc] init];
        self.pageLabel.textColor = [UIColor systemBlueColor];
        self.pageLabel.textAlignment = NSTextAlignmentCenter;
        self.pageLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.pageLabel];
        
        @MK_WEAKIFY(self);
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.pageControl sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            @MK_NORMALIZE(self);
            CGSize s1 = [self.pageLabel intrinsicContentSize];
            size.width -= (s1.width+self.flowlayout.interitemSpacing);
            MKUIPageControl *pageControl = wrapper.originItem;
            CGSize s = [pageControl sizeThatFits:size];
            s.height = MIN(20,s.height);
            return s;
        }];
        MKUILayoutConstraintItemWrapper *w2 = [MKUILayoutConstraintItemWrapper wrapItem:self.pageLabel sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            CGSize s1 = [wrapper.originItem sizeThatFits:size];
            s1.width = MAX(s1.width,40);
            return s1;
        }];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1,w2] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_C) contentInsets:UIEdgeInsetsMake(0, 50, 0, 50) interitemSpacing:5];
    }
    return self;
}
- (void)setHidesForSinglePage:(BOOL)hidesForSinglePage{
    self.pageControl.hidesForSinglePage = hidesForSinglePage;
}
- (BOOL)hidesForSinglePage{
    return self.pageControl.hidesForSinglePage;
}
+ (Class)pageControlClass{
    return [MKUITestImagesPageControl class];
}
- (void)scrollToPageIndex:(NSInteger)pageIndex progress:(CGFloat)progress{
    [self.pageControl scrollToPageIndex:pageIndex progress:progress];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 60);
}
- (void)setCurrentPage:(NSInteger)currentPage{
    _currentPage = currentPage;
    [self reloadData];
}
- (void)setNumberOfPages:(NSInteger)numberOfPages{
    _numberOfPages = numberOfPages;
    [self reloadData];
}
- (void)reloadData{
    self.pageControl.numberOfPages = self.numberOfPages;
    self.pageControl.currentPage = self.currentPage;
    
    self.pageLabel.text = [NSString stringWithFormat:@"%@/%@",@(self.numberOfPages?self.currentPage+1:0),@(self.numberOfPages)];
    [self setNeedsLayout];
}
@end

@implementation TestUIPageControl_1
+ (Class)pageControlClass{
    return [MKUIPageControl class];
}
@end

@implementation TestUIPageControl_2
+ (Class)pageControlClass{
    return [MKUITestImagesPageControl class];
}
@end
@implementation TestUIPageControl_3
+ (Class)pageControlClass{
    return [MKUITestPageControl_1 class];
}
@end
@implementation TestUIPageControl_4
+ (Class)pageControlClass{
    return [MKUITestPageControl_2 class];
}
@end
@implementation TestUIPageControl_5
+ (Class)pageControlClass{
    return [MKUITestPageControl_3 class];
}
@end
